 #define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> ret;
        if (root == nullptr) return ret;
        queue<TreeNode*> q;

        q.push(root);
        while (q.size())
        {
            int sz = q.size();
            int tmp = INT_MIN;
            for (int i = 0; i < sz; i++)
            {
                TreeNode* t = q.front();
                q.pop();
                tmp = max(tmp, t->val);

                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }
            ret.push_back(tmp);
        }
        return ret;
    }
};